A SystemExit
exception is raised when sys.exit()
is called. This exception is used to signal the interpreter to
exit. The exception is expected to propagate up until the program stops. It is possible to catch this exception in order to perform, for example,
clean-up tasks. It should, however, be raised again to allow the interpreter to exit as expected. Not re-raising such exception could lead to
undesired behaviour.
A bare except:
statement, i.e. an
except
block without any exception class, is equivalent to except BaseException
. Both statements will catch every
exceptions, including SystemExit
. It is recommended to catch instead a more specific exception. If it is not possible, the exception
should be raised again.
It is also a good idea to re-raise the KeyboardInterrupt
exception. Similarly to
SystemExit
,KeyboardInterrupt
is used to signal the interpreter to exit. Not re-raising such exception could also lead to
undesired behaviour.